/**
 * 
 */
package unseen.labs.games.mysnake.model;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

/**
 * 
 * @author PERRAUDIN Steven <sperraudin@neotilus.com>
 *
 * @date 4 juin 2010
 */
public class Racket {
	
	private int _nrOfVertices = 0;
	
	// a raw buffer to hold indices allowing a reuse of points.
    private ShortBuffer _indexBuffer;
    
    // a raw buffer to hold the vertices
    private FloatBuffer _vertexBuffer;
    

	public Racket(){
		
		initView();
		
	}
	
	
	private void initView(){
		
		float[] _coords = {
				-0.3f, 	-1f, 	0f, 	// (x1, y1, z1)
		        0.3f, 	-1f, 	0f, 	// (x2, y2, z2)
		        0f, 	-0.9f, 	0f, 	// (x3, y3, z3)

        };
        _nrOfVertices 	= _coords.length;
        
        short[] indices = {0, 1, 2};
		
     // float has 4 bytes, coordinate * 4 bytes
        ByteBuffer vbb = ByteBuffer.allocateDirect(_coords.length * 4);
        vbb.order(ByteOrder.nativeOrder());
        _vertexBuffer = vbb.asFloatBuffer();
		
     // short has 2 bytes, indices * 2 bytes
        ByteBuffer ibb = ByteBuffer.allocateDirect(_nrOfVertices * 2);
        ibb.order(ByteOrder.nativeOrder());
        _indexBuffer = ibb.asShortBuffer();
        
        _vertexBuffer.put(_coords);
        _indexBuffer.put(indices);
        
        _vertexBuffer.position(0);
        _indexBuffer.position(0);
		
	}
	
	public void moveLeft(){
		if(_vertexBuffer.get(3) < 1){
			_vertexBuffer.put(0, _vertexBuffer.get(0) + 0.1f);
			_vertexBuffer.put(3, _vertexBuffer.get(3) + 0.1f);
			_vertexBuffer.put(6, _vertexBuffer.get(6) + 0.1f);
		}
	}

	public void moveRight(){
		if(_vertexBuffer.get(3) > 0.1){
			_vertexBuffer.put(0, _vertexBuffer.get(0) - 0.1f);
			_vertexBuffer.put(3, _vertexBuffer.get(3) - 0.1f);
			_vertexBuffer.put(6, _vertexBuffer.get(6) - 0.1f);
		}
	}
	
	public int isBallOnBoard(float x, float y){
		
		if(x > _vertexBuffer.get(0) && x > _vertexBuffer.get(6)){
			return 1;
		}
		return 0;
	}

	/**
	 * @return the _nrOfVertices
	 */
	public int getNrOfVertices() {
		return _nrOfVertices;
	}


	/**
	 * @param nrOfVertices the _nrOfVertices to set
	 */
	public void setNrOfVertices(int nrOfVertices) {
		_nrOfVertices = nrOfVertices;
	}


	/**
	 * @return the _indexBuffer
	 */
	public ShortBuffer getIndexBuffer() {
		return _indexBuffer;
	}


	/**
	 * @param indexBuffer the _indexBuffer to set
	 */
	public void setIndexBuffer(ShortBuffer indexBuffer) {
		_indexBuffer = indexBuffer;
	}


	/**
	 * @return the _vertexBuffer
	 */
	public FloatBuffer getVertexBuffer() {
		return _vertexBuffer;
	}


	/**
	 * @param vertexBuffer the _vertexBuffer to set
	 */
	public void setVertexBuffer(FloatBuffer vertexBuffer) {
		_vertexBuffer = vertexBuffer;
	}
	
	
}
